﻿using System.Collections.Generic;
using System.Data;
using MedicService.Domain;
using MedicService.Domain.Directory;
using MedicService.Server.Factory.Directory;
using MedicService.Server.Factory.Directory.Simple;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class GroupExpFactory : EntityFactory<GroupExp> {
        public GroupExpFactory() : base("GroupExp") { }
        protected override GroupExp CreateEntity() {
            return new GroupExp();
        }

        protected override string GetInsertQuery(GroupExp entity) {
            string q = "INSERT INTO [GroupExp]" +
                       "([group]" +
                       ",[chairman]" +
                       ",[secretary]" +
                       ",[SpecialityList])" +
                       "VALUES (" 
                       + Nullable(entity.Group)
                       + ", " + Nullable(entity.Chairman)
                       + ", " + Nullable(entity.Secretary) 
                       + ", " + Nullable(entity.SpecialityList) + "); " +
                       "";
            return q;
        }

        protected override string GetUpdateQuery(GroupExp entity) {
            string q = "UPDATE [GroupExp]" +
                       "SET [group] = '" + entity.Group + "'"+
                       " ,[chairman] = '" + entity.Chairman + "'" +
                       " ,[secretary] = '" + entity.Secretary + "'" +
                       " ,[SpecialityList] = " + Nullable(entity.SpecialityList) +
                       " WHERE id = " + entity.Id;
            return q;
        }

        protected override void FillEntity(GroupExp entity, DataRow myDataRow) {
            entity.Id = (int) myDataRow["id"];
            entity.Chairman = (string) myDataRow["chairman"];
            entity.Group = (string)myDataRow["group"];
            entity.Secretary = (string)myDataRow["secretary"];
            List<int> idList = ParseIdList(myDataRow["SpecialityList"] as string);
            entity.SpecialityList = new List<Speciality>();
            var specialityFactory = new SpecialityFactory();
            foreach (var id in idList) {
                entity.SpecialityList.Add(specialityFactory.Load(id));
            }
        }
    }
}